import Mock from 'mockjs'
import StringUtils from '../../utils/stringUtils.js' 

let menu = []
let List =[
	{
		id: 99999,
		pid: 0,
		menuname: '首页',
		icon: 's-home',
		perms: 'home',
		path: 'home/index',
		routename: 'home',
	},
	{
		id: 1,
		pid: 0,
		menuname: '系统管理',
		icon: 'setting',
		perms: 'system',
		path: 'system',
		routename: ''
	},
	{
		id: 11,
		pid: 1,
		menuname: '用户管理',
		icon: 'user',
		perms: 'user',
		path: 'user/index',
		routename: 'user'
	},
	{
		id: 12,
		pid: 1,
		menuname: '角色管理',
		icon: 'role',
		perms: 'role',
		path: 'role/index',
		routename: 'role'
	},
	{
		id: 13,
		pid: 1,
		menuname: '菜单管理',
		icon: 'menu',
		perms: 'menu',
		path: 'menu/index',
		routename: 'menu'
	},
	{
		id: 2,
		pid: 0,
		menuname: '业务管理',
		icon: 'video-play',
		perms: 'bill',
		path: 'bill',
		routename: ''
	},
	{
		id: 21,
		pid: 2,
		menuname: '商品管理',
		icon: 'mall',
		perms: 'mall',
		path: 'mall/index',
		routename: 'mall'
	}, 
	{
		id: 22,
		pid: 2,
		menuname: '客户管理',
		icon: 'customer',
		perms: 'customer',
		path: 'customer/index',
		routename: 'customer'
	}
]

function getChildMenu(item){
	let submenu = menu.filter(s=>{
		if(s.pid != item.id) return false
		return true
	})
	if(submenu.length>0){
		submenu.forEach(t=>{
			getChildMenu(t)
		})
		item.children = submenu
		// console.log(JSON.stringify(item))
	}
}
			
export default {
	getMenuTree: config =>{
		menu = []
		let menuList = []
		List.forEach(t=>{
			if(t.menuname != '首页'){
				let item = {
					id: t.id,
					pid: t.pid,
					name: t.routename,
					label: t.menuname,
					path: t.path,
					url: t.path,
					icon: t.icon
				}
				menu.push(item)
			}
		})
		menu.forEach(t=>{
			if(t.pid == 0){
				getChildMenu(t)
				menuList.push(t)
			}
		})
		
		return {
			code: 20000,
			count: menu.length,
			list: menuList
		}
	},
	
	getMenu: config => {
		let r = []
		menu = []
		List.forEach(t=>{
			let item = {
				id: t.id,
				pid: t.pid,
				name: t.routename,
				label: t.menuname,
				path: t.path,
				url: t.path,
				icon: t.icon
			}
			menu.push(item)
		})
		const {username, password} = JSON.parse(config.body)
		//先判断用户是否存在
		//判断账号密码是否对应
		if(username === 'admin' && password === 'admin') {
			menu.forEach(t=>{
				if(t.pid == 0){
					getChildMenu(t)
					r.push(t)
				}
			})
			return {
				code: 20000,
				message: '获取成功',
				token: Mock.Random.guid(),
				data: {
					menu: r,
					// [
					// 	{
					// 		path: '/home',
					// 		name: 'home',
					// 		label: '首页',
					// 		icon: 's-home',
					// 		url: 'home/index'
					// 	},
					// 	{
					// 		label: '系统设置',
					// 		icon: 'setting',
					// 		children: [
					// 			{
					// 				path: '/user',
					// 				name: 'user',
					// 				label: '用户管理',
					// 				icon: 'user',
					// 				url: 'user/index'
					// 			},
					// 			{
					// 				path: '/role',
					// 				name: 'role',
					// 				label: '角色管理',
					// 				icon: 'setting',
					// 				url: 'role/index.vue'
					// 			},
					// 			{
					// 				path: '/menu',
					// 				name: 'menu',
					// 				label: '菜单管理',
					// 				icon: 'video-play',
					// 				url: 'menu/index.vue'
					// 			},
					// 		]
					// 	}
					// ],

				}
			}
		}
		else{
			if(username === 'wlq' && password === 'wlq'){
				menu.forEach(t=>{
					if(t.pid == 0 && t.id != 1){
						getChildMenu(t)
						r.push(t)
					}
				})
				return {
					code: 20000,
					data: {
						menu: r,
						token: Mock.Random.guid(),
						message: '获取成功'
					}
				}
			}
			else{
				return {
					code: -999,
					data: {
						message: '密码错误'
					}
				}
			}
			
		}
	},
	
	getMenuList: config =>{
		const {menuname} = StringUtils.param2Obj(config.url)
		const mockList = List.filter(menu => {
			if(menuname && menu.menuname.indexOf(menuname) === -1) return false
			return true
		})
		return {
			code: 20000,
			list: mockList
		}
	},
	
	addMenu: config => {
		const { menuname, icon, path, perms,pid } = JSON.parse(config.body)
		List.unshift({
			id: Mock.Random.guid(),
			pid: pid,
			menuname: menuname,
			icon: icon,
			path: path,
			perms: perms
		})
		return {
			code: 20000,
			data: {
				message: '添加成功'
			}
		}
	},
	
	updateMenu: config => {
		const { id, pid, menuname, icon, path, perms, children } = JSON.parse(config.body)
		List.some(u=>{
			if(u.id === id) {
				u.pid = pid
				u.menuname = menuname
				u.icon = icon
				u.path = path
				u.perms = perms
				return true
			}
		})
		return {
			code: 20000,
			data: {
				message: '编辑成功'
			}
		}
	},
	
	delMenu: config => {
		const {id} =JSON.parse(config.body) 
		if (!id){
			return {
				code: -999,
				message: '参数不正确'
			}
		}
		else{
			List = List.filter(u=>u.id !== id)
			return {
				code: 20000,
				message: '删除成功'
			}
		}
	},
	
}

